package com.moxiao.yfjh.module.order.tradeRefund.payRecord;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.module.order.controller.admin.payRecord.vo.PayRecordCreateReqVO;
import com.moxiao.yfjh.module.order.controller.admin.payRecord.vo.PayRecordExportReqVO;
import com.moxiao.yfjh.module.order.controller.admin.payRecord.vo.PayRecordPageReqVO;
import com.moxiao.yfjh.module.order.controller.admin.payRecord.vo.PayRecordUpdateReqVO;
import com.moxiao.yfjh.module.order.convert.payRecord.PayRecordConvert;
import com.moxiao.yfjh.module.order.dal.dataobject.payRecord.PayRecordDO;
import com.moxiao.yfjh.module.order.dal.mysql.payRecord.PayRecordMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static com.moxiao.yfjh.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.moxiao.yfjh.module.order.enums.ErrorCodeConstants.PAY_RECORD_NOT_EXISTS;

/**
 * 支付记录 Service 实现类
 *
 * @author mx
 */
@Service
@Validated
public class PayRecordServiceImpl implements PayRecordService {

    @Resource
    private PayRecordMapper payRecordMapper;

    @Override
    public Long createPayRecord(PayRecordCreateReqVO createReqVO) {
        // 插入
        PayRecordDO payRecord = PayRecordConvert.INSTANCE.convert(createReqVO);
        payRecordMapper.insert(payRecord);
        // 返回
        return payRecord.getId();
    }

    @Override
    public void updatePayRecord(PayRecordUpdateReqVO updateReqVO) {
        // 校验存在
        validatePayRecordExists(updateReqVO.getId());
        // 更新
        PayRecordDO updateObj = PayRecordConvert.INSTANCE.convert(updateReqVO);
        payRecordMapper.updateById(updateObj);
    }

    @Override
    public void deletePayRecord(Long id) {
        // 校验存在
        validatePayRecordExists(id);
        // 删除
        payRecordMapper.deleteById(id);
    }

    private void validatePayRecordExists(Long id) {
        if (payRecordMapper.selectById(id) == null) {
            throw exception(PAY_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public PayRecordDO getPayRecord(Long id) {
        return payRecordMapper.selectById(id);
    }

    @Override
    public List<PayRecordDO> getPayRecordList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return payRecordMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<PayRecordDO> getPayRecordPage(PayRecordPageReqVO pageReqVO) {
        return payRecordMapper.selectPage(pageReqVO);
    }

    @Override
    public List<PayRecordDO> getPayRecordList(PayRecordExportReqVO exportReqVO) {
        return payRecordMapper.selectList(exportReqVO);
    }

}
